package demo.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/session")
public class SessionController {
	
    private static final String NAME = "time";

	
	@RequestMapping("/get")
	public Map<String,Object> getSessionInfo(HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();		
		map.put("class", session.getClass().toString());	
		String id = session.getId();
		map.put("id", id);
		map.put("createTime", session.getCreationTime());
		map.put("maxInactiveInterval", session.getMaxInactiveInterval());
		map.put("lastAccessedTime", session.getLastAccessedTime());
		map.put("sessionValue", session.getAttribute(NAME));

		return map;
	}
	
	
	@RequestMapping("/update")
	public Map<String,Object> getUpdateInfo(HttpSession session){
		
		Map<String,Object> map = new HashMap<String,Object>();		
		map.put("class", session.getClass().toString());	
		String id = session.getId();
		map.put("id", id);
		map.put("createTime", session.getCreationTime());
		map.put("maxInactiveInterval", session.getMaxInactiveInterval());
		map.put("lastAccessedTime", session.getLastAccessedTime());
		
		Object old = session.getAttribute(NAME);
		
		map.put("sessionValueOld", old);
		
		Object newValue = LocalDateTime.now().toString();
		
		session.setAttribute(NAME, newValue);
		
		map.put("sessionValue", newValue);


		return map;
	}
	
	@RequestMapping("/clear")
	public Map<String,Object> clear(HttpSession session){
		
		Map<String,Object> map = new HashMap<String,Object>();	
		
		
		map.put("sessionValue", session.getAttribute(NAME));
		
		session.invalidate();
		
		map.put("data", "session clear at "+LocalDateTime.now());

		
		return map;
		
	}
	
	

	
	
	
}